<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="100px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="brief">
        <el-input type="textarea" :rows="2" v-model="dataForm.brief" placeholder="请输入简介"></el-input>
      </el-form-item>
      <el-form-item label="图片" prop="picUrl">
        <single-image-upload prcScale="800x600" module="picUrl" v-model="dataForm.picUrl"></single-image-upload>
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input type="textarea" :rows="2" v-model="dataForm.note" placeholder="请输入备注"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleImageUpload from '@/components/UploadImage/SingleImageUpload';
import { getCustomInfo, createCustom, updateCustom } from '@/api/custom/custom';

const defaultDataForm = {
  id: '',
  title: '',
  brief: '',
  picUrl: '',
  status: '',
  note: '',
}

export default {
  components: {
    SingleImageUpload,
  },
  data() {
    return {
      dataForm: Object.assign({}, defaultDataForm),
      visible: false,
      dataRule: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        brief: [
          { required: true, message: '简介不能为空', trigger: 'blur' }
        ],
        picUrl: [
          { required: true, message: '风采图片不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ],
        note: [
          { required: true, message: '备注不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getCustomInfo(this.dataForm.id).then(res => {
            if (res && res.code === 200) {
                this.dataForm.title = res.data.title;
                this.dataForm.brief = res.data.brief;
                this.dataForm.picUrl = res.data.picUrl;
                this.dataForm.status = res.data.status;
                this.dataForm.note = res.data.note;
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let params = {
            id: this.dataForm.id,
            title: this.dataForm.title,
            brief: this.dataForm.brief,
            picUrl: this.dataForm.picUrl,
            status: this.dataForm.status,
            note: this.dataForm.note,
          }
          if (!this.dataForm.id) {
            // 创建
            createCustom(params).then(response => {

              this.$refs['dataForm'].resetFields();
              this.dataForm = Object.assign({}, defaultDataForm);
              this.$message({
                message: '提交成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false
              this.$emit('refreshDataList')

            })
          } else {
            // 更新
            updateCustom(params).then(response => {

              this.$refs['dataForm'].resetFields();
              this.$message({
                message: '修改成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false
              this.$emit('refreshDataList')

            })
          }
        }
      })
    },
  }
}
</script>
